﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.DataAccess.Common
{
    public static class ExceptionExtension
    {
        public static string GetFullStackTrace(this Exception e)
        {
            return e.ToString();
            //return CustomErrorStackTrace(ref e);
        }

        private static string CustomErrorStackTrace(ref Exception e)
        {
            int stackDepth = 0;
            StringBuilder sb = new StringBuilder();
            do
            {
                if (stackDepth == 0)
                    sb.Append("Exception==========================================>\r\n");
                else
                {
                    sb.Append($"{stackDepth} depth Exception==========================================>\r\n");
                }
                sb.Append($"ErrorMsg:{e.Message}\r\nErrorStack:\r\n{e.StackTrace}\r\n");
                e = e.InnerException;
                stackDepth++;
            } while (e != null);
            return sb.ToString();
        }

        /// <summary>
        /// 获取内部异常信息
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static string GetInnermostExceptionMsg(this Exception e)
        {
            while(e.InnerException != null)
            {
                e = e.InnerException;
            }
            return e.Message;
        }
    }
}